package com.zqq.baselibrary.ui.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.util.Log
import com.alibaba.android.arouter.launcher.ARouter
import com.bigkoo.alertview.AlertView
import com.bigkoo.alertview.OnItemClickListener
import com.jph.takephoto.app.TakePhoto
import com.jph.takephoto.app.TakePhotoImpl
import com.jph.takephoto.compress.CompressConfig
import com.jph.takephoto.model.TResult
import com.zqq.baselibrary.common.BaseApplication
import com.zqq.baselibrary.injection.component.ActivityComponent
import com.zqq.baselibrary.injection.component.DaggerActivityComponent
import com.zqq.baselibrary.injection.module.ActivityModule
import com.zqq.baselibrary.injection.module.LifecycleProviderModule
import com.zqq.baselibrary.presenter.BasePresenter
import com.zqq.baselibrary.presenter.view.BaseView
import com.zqq.baselibrary.utils.DateUtils
import com.zqq.baselibrary.widgets.ProgressLoading
import org.jetbrains.anko.toast
import java.io.File
import javax.inject.Inject

/**
 * 存在选择图片的Activity基础封装
 * wc
 * @author zqq on 2018/7/30
 */
abstract class BaseTakePhotoActivity<T : BasePresenter<*>> : BaseActivity(), BaseView, TakePhoto.TakeResultListener {


    private lateinit var mTakePhoto: TakePhoto
    private lateinit var mTempFile: File

    @Inject
    lateinit var mPresenter: T
    lateinit var mActivityComponent: ActivityComponent
    private lateinit var mLoadingDialog: ProgressLoading

    //Dagger注册
    abstract fun injectComponent()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initActivityInjection()
        injectComponent()

        mTakePhoto = TakePhotoImpl(this, this)
        mTakePhoto.onCreate(savedInstanceState)

        mLoadingDialog = ProgressLoading.create(this)
        ARouter.getInstance().inject(this)
    }

    /**
     * 实例化ActivityComponent
     */
    private fun initActivityInjection() {
        mActivityComponent = DaggerActivityComponent.builder()
                .appComponent((application as BaseApplication).appComponent)
                .activityModule(ActivityModule(this))
                .lifecycleProviderModule(LifecycleProviderModule(this))
                .build()
    }

    /* 弹出选择框，默认实现 可根据实际情况，自行修改 */
    protected fun showAlertView() {
        AlertView("选择图片", "", "取消", null,
                arrayOf("拍照", "相册"), this, AlertView.Style.ActionSheet,
                OnItemClickListener { _, position ->
                    //没次都要对图片进行压缩
                    mTakePhoto.onEnableCompress(CompressConfig.ofDefaultConfig(), false)

                    when (position) {
                        0 -> {
                            createTempFile()
                            mTakePhoto.onPickFromCapture(Uri.fromFile(mTempFile))
                        }
                        1 -> mTakePhoto.onPickFromGallery()
                    }
                }).show()
    }

    //获取URI--新建临时文件
    fun createTempFile() {
        val tempFileName = "${DateUtils.curTime}.png"
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            this.mTempFile = File(Environment.getExternalStorageDirectory(), tempFileName)
            return
        }
        this.mTempFile = File(filesDir, tempFileName)
    }


    /*----------takePhoto返回接口--------------*/
    //获取图片，成功回调
//    override fun takeSuccess(result: TResult?) {
//        Log.e("TakePhoto", result?.image?.originalPath)
//        Log.e("TakePhoto", result?.image?.compressPath)
//    }

    //获取图片，取消回调
    override fun takeCancel() {
    }

    //获取图片，失败回调
    override fun takeFail(result: TResult?, msg: String?) {
        Log.e("takePhoto", msg)
    }

    //TakePhoto默认实现
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        mTakePhoto.onActivityResult(requestCode, resultCode, data)
    }


    /*-------------BaseView接口返回----------*/
    override fun showLoading() {
        mLoadingDialog.showLoading()
    }

    override fun hideLoading() {
        mLoadingDialog.hideLoading()
    }

    override fun onError(text: String) {
        toast(text)
    }

}